var utils = require('../../../utils/util.js')
var c = require('../../../utils/config.js')
var s = require('../../../utils/sign.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        star: [
            '/static/images/shop/star3.png',
            '/static/images/shop/star3.png',
            '/static/images/shop/star3.png',
            '/static/images/shop/star3.png',
            '/static/images/shop/star3.png'
        ],
        star_num:3,
        rating: '未评价',
        orderExt: [],
        grade: -1,
        content: "",
        img_url: c.img_url,
        pics: [],
        img_list: []
    },
    onLoad: function (options) {
        var orderExt = JSON.parse(options.orderExt);
        this.setData({
            orderExt: orderExt
        });
    },
    star_rating: function (e) {
        let len = e.currentTarget.dataset.index;
        this.data.star.forEach((item,i) => {
            let str = "star[" + i + "]";
            this.setData({
                [str]: '/static/images/shop/star3.png'
            })
        });
        for (let i = 0; i < len+1; i++) {
            let str = "star[" + i + "]";
            this.setData({
              [str]: '/static/images/shop/star2.png'
            })
        }
        if (len == 2) {
            this.setData({
                rating: '中评',
                grade: len
            })
        } else if (len > 2) {
            this.setData({
                rating: '好评',
                grade: len
            })
        } else {
            this.setData({
                rating: '差评',
                grade: len
            })
        }

    },
    editContent: function (e) {
        this.setData({
            content: e.detail.value
        })
    },
    updata: function () {
        var imgList = this.data.img_list;
        if (imgList.length == 3){
            utils.errMsg("最多只能上传3张图片");
            return false;
        }
        var that = this;
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var tempFilePaths = res.tempFilePaths;
                var data = {
                    url: '/zfcom/attachment/upimg',
                    partner: 5500,
                };
                data['sign'] = s.sign(data);
                wx.uploadFile({
                    url: c.api_url,
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: data,
                    success: function (res) {

                        var data = JSON.parse(res.data);
                        if (data.code == 0) {
                            var imgList = that.data.img_list;
                            var img_data = { "src": tempFilePaths[0],"url":data.data.url};
                            imgList.push(img_data);
                            that.setData({
                                img_list: imgList
                            });
                        } else {

                            utils.errMsg(data.msg);
                        }
                    }
                })
            }
        })
    },
    delImg:function(e){
        var index = e.currentTarget.dataset.index;
        var img_list = this.data.img_list;
        img_list.splice(index, 1);
        this.setData({
            img_list: img_list
        })
    },

    submit: function () {
        var grade = this.data.grade+1;
        if (grade <= 0) {
            utils.errMsg("请选择评级等级!");
            return;
        }
        var that = this;
        var token = wx.getStorageSync("userinfo").wap_token;
        var imgList = that.data.img_list;
        var pics = "";
        if (imgList.length>0){
            imgList.forEach((item, index) => {
                pics += item.url+",";
            })
            pics = pics.substr(0, pics.length - 1);
        }
        var data = {
            url: "/wsgoods/goodsevaluate/add",
            token: token,
            order_goods_id: this.data.orderExt.id,
            goods_id: this.data.orderExt.goods_id,
            goods_price_id: this.data.orderExt.goods_price_id,
            grade: grade,
            content: this.data.content,
            pics: pics,
            anonymous: this.data.anonymous
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                utils.sucMsg("评价成功");
                setTimeout(function () {
                    wx.navigateBack({
                        delta: 1
                    })
                }, 1500);

            } else {
                utils.errMsg(res.msg);
            }
        });
    }
})
